function editXML(xmlStr, newValue) {
  const parser = new DOMParser();
  const xmlDoc = parser.parseFromString(xmlStr, 'text/xml');
  Object.keys(newValue).map((i) => {
    const taskNode = xmlDoc.getElementById(i);
    Array.from(taskNode.childNodes)
      .filter((taskChild) => taskChild.nodeName === 'bpmn2:extensionElements') // filter extension elements
      .flatMap((taskChild) => Array.from(taskChild.childNodes)) // get extension nodes
      .filter(((extensionNode) => extensionNode.nodeName === 'camunda:properties')) // filter properties
      .map((extensionNode) => Array.from(extensionNode.childNodes)) // get property nodes
      .map((propertyNodes) => propertyNodes
        .filter((propertyNode) => propertyNode.nodeType === 1) // filter nodeType === 1
        .map((propertyNode) => {
          switch (propertyNode.attributes.name.nodeValue) {
            case 'arguments': {
              const paramNodes = Array.from(propertyNode.childNodes);
              paramNodes
                .filter((node) => node.nodeType === 1) // filter nodeType === 1
                .map((node) => {
                  const re = node;
                  Object.keys(newValue[i].params).map((param) => {
                    if (node.attributes.name.nodeValue === param) {
                      re.attributes.value.nodeValue = newValue[i].params[param];
                    }
                    return 0;
                  });
                  return re;
                });
              break;
            }
            case 'instances': {
              const instances = Array.from(propertyNode.childNodes);
              instances
                .filter((node) => node.nodeType === 1) // filter nodeType === 1
                .map((node) => {
                  const re = node;
                  if (newValue[i].instances.indexOf(node.attributes.name.nodeValue) !== -1) {
                    re.attributes.value.nodeValue = 'true';
                  }
                  return re;
                });
              break;
            }
            default:
          }
          return 0;
        }));
    return 0;
  });
  const newXmlStr = (new XMLSerializer()).serializeToString(xmlDoc);
  return newXmlStr;
}

function editTaskName(xmlStr) {
  const parser = new DOMParser();
  const xmlDoc = parser.parseFromString(xmlStr, 'text/xml');
  const taskNodes = xmlDoc.getElementsByTagName('bpmn2:task');
  Array.from(taskNodes).map((taskNode) => {
    const re = taskNode;
    re.attributes.name.nodeValue = taskNode.attributes.id.nodeValue;
    return re;
  });
  const newXmlStr = (new XMLSerializer()).serializeToString(xmlDoc);
  return newXmlStr;
}


export {
  editXML,
  editTaskName,
};
